package com.bwie.controller;

import com.bwie.domain.TbUser;
import com.bwie.poi.ExcelUtil;
import com.bwie.service.TbUserService;
import com.bwie.utils.R;
import com.bwie.vo.CheckUserVo;
import com.bwie.vo.LoginInfoVo;
import com.bwie.vo.PageInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 军哥
 * @version 1.0
 * @description: TODO
 * @date 2024/10/15 16:04
 */

@RestController
@RequestMapping(value = "/user/user")
@Slf4j
public class TbUserController {

    @Autowired
    TbUserService tbUserService;

    /**
     * 发送验证码
     * @param mobile
     * @return
     */
    @PostMapping(value = "/sendCode/{mobile}")
    public R sendCode(@PathVariable("mobile") String mobile) {
        return tbUserService.sendCode(mobile);
    }

    /**
     * 手机号用户登录
     * @param loginInfoVo
     * @return
     */
    @PostMapping(value = "/login")
    public R login(@RequestBody LoginInfoVo loginInfoVo) {
        return tbUserService.login(loginInfoVo);
    }

    /**
     * 账号用户登录
     * @param loginInfoVo
     * @return
     */
    @PostMapping(value = "/loginByName")
    public R loginByName(@RequestBody LoginInfoVo loginInfoVo) {
        return tbUserService.loginByName(loginInfoVo);
    }

    @GetMapping(value = "/getUserById/{userId}")
    public R getUserById(@PathVariable("userId") Integer userId) {
        return tbUserService.getUserById(userId);
    }

    /**
     * 商户注册接口
     * @param tbUser
     * @return
     */
    @PostMapping(value = "/shoperRegister")
    public R shoperRegister(@RequestBody TbUser tbUser) {
        return tbUserService.shoperRegister(tbUser);
    }

    @PostMapping(value = "/getUserList")
    public R getUserList(@RequestBody PageInfoVo pageInfoVo) {
        return tbUserService.getUserList(pageInfoVo);
    }

    @GetMapping(value = "/exportUserList")
    public void exportUserList(HttpServletResponse response) {
        // 获取数据的列表
        List<TbUser> tbUserList = tbUserService.getAllUserList(2);

        // 导出列表
        ExcelUtil<TbUser> excelUtil = new ExcelUtil<>(TbUser.class);
        excelUtil.exportExcel(response, tbUserList, "用户数据");
    }

    /**
     * 从ES中分页查询用户信息
     * @param pageInfoVo
     * @return
     */
    @PostMapping(value = "/getUserListFromEs")
    public R getUserListFromEs(@RequestBody PageInfoVo pageInfoVo) {
        return tbUserService.getUserListFromEs(pageInfoVo);
    }

    /**
     * 商家审核
     * @return
     */
    @PostMapping(value = "/checkUser")
    public R checkUser(@RequestBody CheckUserVo checkUserVo) {
        System.out.println(checkUserVo);
        return tbUserService.checkUser(checkUserVo);
    }

    @PostMapping(value = "/subUserMoney/{userId}/{userMoney}")
    public R subUserMoney(@PathVariable("userId") Integer userId, @PathVariable("userMoney") Double userMoney) {
        return tbUserService.subUserMoney(userId, userMoney);
    }
}
